﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;

namespace UI
{
    public enum IconType
    {
        NotLight=0,
        WillLight=1,
        HasLight=2,
    }
    public class UIYggdrasilIcon : MonoBehaviour
    {
        public IconType iconType;
        public UISprite mainIcon;
        public GameObject effect;
        public TweenScale inScale;
        public TweenPosition inPos;

        public int index;
        public UIYggdrasil page;

        private const string mdef = "UI_shuxing_item__fafang_";
        private const string pdef = "UI_shuxing_item__wufang_";
        private const string att  = "UI_shuxing_item__gongji_";
        private const string hp   = "UI_shuxing_item__shengming_";
        private const string quility = "UI_shuxing_item__tisheng_";
        private const string item = "UI_shuxing_item__jiangli_";

        private string[] states = new string[3] { "a","c","b" };


        public void Init(TableWorldSeed seed,IconType iconType,int index,UIYggdrasil page)
        {
            this.iconType = iconType;
            this.index = index;
            this.page = page;

            InitIcon(seed);
            InitEffect();
            InitAnimation();
        }

        void InitIcon(TableWorldSeed seed)
        {
            string str = "";
            if (seed.type == 1)
            {
                var ss = seed.get.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
                if (ss[0] == Dict.Config.TableConst.Attack)
                {
                    str = att;
                }
                else if (ss[0] == Dict.Config.TableConst.MagicDefend)
                {
                    str = mdef;
                }
                else if (ss[0] == Dict.Config.TableConst.PhyDefend)
                {
                    str = pdef;
                }
                else if (ss[0] == Dict.Config.TableConst.Hp)
                {
                    str = hp;
                }

            }
            else if (seed.type == 2)
            {
                str = item;
            }
            else if (seed.type == 3)
            {
                str = quility;
            }

            mainIcon.spriteName = str + states[(int)iconType];
            mainIcon.alpha = 1f;

            if(!page.firstOpen)
            {
                if (iconType == IconType.WillLight)
                {
                    mainIcon.spriteName = str + states[(int)IconType.NotLight];
                    StartCoroutine(_WillLightEffect(str));
                }
                page.firstOpen = false;
            }

            
        }

        IEnumerator _WillLightEffect(string str)
        {
            if(index>0)
            yield return new WaitForSeconds(1.5f);
            mainIcon.spriteName = str + states[(int)IconType.WillLight];
        }
        
        void InitEffect()
        {
            if(iconType== IconType.WillLight)
            {
                effect.SetActive(true);
            }
            else
            {
                effect.SetActive(false);
            }
        }

        void InitAnimation()
        {
            if (iconType == IconType.WillLight)
            {
                inScale.enabled = true;
                inPos.enabled = false;
            }
            else if (iconType == IconType.HasLight)
            {
                inScale.enabled = false;
                inPos.enabled = true;
            }
            else
            {
                inScale.enabled = false;
                inPos.enabled = false;
            }
        }
    }
}
